package com.exam.service.serviceImpl;

import com.exam.dataobject.Options;
import com.exam.dataobject.Question;
import com.exam.dataobject.mapper.QuestionMapper;
import com.exam.enums.ResultEnum;
import com.exam.exception.ExaminationException;
import com.exam.repository.OptionsRepository;
import com.exam.repository.QuestionRepository;
import com.exam.service.QuestionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by v
 * Date:2018/12/6
 * Time:18:52
 */
@Service
@Slf4j
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private OptionsRepository optionsRepository;

    @Override
    public void insertQuestion(Map<String, Object> map) {
//        questionMapper.insertQuestion(Map);
        Question question = new Question();
        question.setContent(map.get("content").toString());
        question.setSubjectId(Integer.valueOf(map.get("subjectId").toString()));
        question.setScore(Integer.valueOf(map.get("score").toString()));
        questionRepository.save(question);

        Options options = new Options();
        options.setQuestionId(question.getQuestionId());
        options.setOpAa(map.get("opAa").toString());
        options.setOpBb(map.get("opBb").toString());
        options.setOpCc(map.get("opCc").toString());
        options.setOpDd(map.get("opDd").toString());
        options.setOpRights(map.get("opRights").toString());

         optionsRepository.save(options);
    }


    @Override
    public void modifyQuestion(Map<String, Object> map) {

        if (map.get("questionId").equals(null)) {
            log.error("【未选择一项题目内容】");
            throw new ExaminationException(ResultEnum.QUESTION_ID_ERROR.getCode(), ResultEnum.QUESTION_ID_ERROR.getMessage());
        }
        Question question = questionRepository.findByQuestionId(Integer.valueOf(map.get("questionId").toString()));

        if (question.getUsedTimes() >= 1){
            log.error("【题目已被引用，不可修改】");
            throw new ExaminationException(ResultEnum.QUESTION_ID_ERROR.getCode(), ResultEnum.QUESTION_ID_ERROR.getMessage());
        }

        Options options = optionsRepository.findByQuestionId(Integer.valueOf(map.get("questionId").toString()));

        if (map.get("content").toString().length() != 0){
            question.setContent(map.get("content").toString());
        }
        if (map.get("subjectId").toString().length() != 0){
            question.setSubjectId(Integer.valueOf(map.get("subjectId").toString()));
        }
        if (map.get("score").toString().length() != 0){
            question.setScore(Integer.valueOf(map.get("score").toString()));
        }
        if (map.get("opAa").toString().length() != 0){
            options.setOpAa(map.get("opAa").toString());
        }
        if (map.get("opBb").toString().length() != 0){
            options.setOpBb(map.get("opBb").toString());
        }
        if (map.get("opCc").toString().length() != 0){
            options.setOpCc(map.get("opCc").toString());
        }
        if (map.get("opDd").toString().length() != 0){
            options.setOpDd(map.get("opDd").toString());
        }
        if (map.get("opRights").toString().length() != 0){
            options.setOpRights(map.get("opRights").toString());
        }

        questionRepository.save(question);
        optionsRepository.save(options);
    }

    @Override
    public PageInfo<Map<String, Object>> getQuestionList(Integer page, Integer size) {

        PageHelper.startPage(page, size);
        Page<Map<String, Object>> all = questionMapper.getQuestionList();
        return all.toPageInfo();
    }
}
